import { defineConfig, loadEnv } from 'vite';
import { viteVConsole } from 'vite-plugin-vconsole';
import removeConsole from "vite-plugin-remove-console";

import progress from 'vite-plugin-progress';//够级进度条插件

import colors from 'picocolors'
import { viteMockServe } from 'vite-plugin-mock';
import vue from '@vitejs/plugin-vue';
import dayjs from 'dayjs';
import path from 'path';
// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  // 启动时根据 command 和 mode 加载 env 内容，全局生效
  const env = loadEnv(mode, "./env",'VITE_');
  // 启动时打印 key 的内容
  //打包文件名称
  const PACKAGE_NAME = process.env.npm_package_version + "-" + dayjs().format('YYYYMMDD') ;
  //打包文件输出文件夹
  const OUT_DIR = "dist/" + PACKAGE_NAME + "/";
  return {
  	base: './',
	//静态资源服务的文件夹
	publicDir: "public",
	envDir: './env',//加载 .env 文件的目录
	json:{stringify:true},
  	resolve: {
  		alias: {
  			'@': path.resolve('src')
  		}
  	},
  	plugins: [
		vue(),
		removeConsole({
			includes: ["log", "warn"],
		}),
 		viteVConsole({
		 	entry: path.resolve('src/main.js'),		// 入口文件
		 	localEnabled: mode === 'development' || mode === 'debug',	// 本地是否启用
		 	enabled: mode === 'development' || mode === 'debug',		// 是否启用
		 	config: {
				maxLogNumber: 1000,
				theme: 'light'						// 主题颜色 'dark'|'light'
			}
		}),
		progress({//构建进度条
			format: `${colors.green(colors.bold('构建中...'))} ${colors.blue('[:bar]')} ${colors.green(colors.bold(':percent ,构建耗时（秒）[:elapsed] ,预计剩余时间（秒）[:eta]'))}`,
			total: 100,
			width: 50,
			callback:()=> {
				console.log('\n');
				console.log(`${colors.cyan('===============================================================================================')}`);
				console.log(`${colors.green('├─ 构建日期:[' + dayjs().format('YYYYMMDD') + ']')}`);
				console.log(`${colors.green('├─ 构建环境:[' + mode + ']')}`);
				console.log(`${colors.green('├─ 输出目录:[' + OUT_DIR + ']')}`);
				console.log(`${colors.green('├─ 打包名称:[' + PACKAGE_NAME + ']')}`);
				console.log(`${colors.cyan('===============================================================================================')}`);
			}
		}),	
	],
	build:{
		outDir: OUT_DIR + PACKAGE_NAME,
		assetsDir:"static"
	}
  }
})
